// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace Northwind.DomainModel.Data
{

    /// <summary />
    public partial class ProductMapper
    {

        /// <summary />
        static ProductMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Product> _FindAll(DataMapperCollection dataMappers)
        {
            IDbCommand[] commands=new IDbCommand[1];

            commands[0]=Connection.CreateCommand();
            commands[0].CommandType=CommandType.Text;
            commands[0].CommandText=Resources.Sql.ProductSelectAll;

            return Load(commands, dataMappers);
        }

        private Product.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Product.DataTransfer data=new Product.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Product.DataTransfer FillData(Product.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("ProductID");
            if (!record.IsDBNull(ord))
                data.ProductID=record.GetInt32(ord);

            ord=record.GetOrdinal("CategoryID");
            if (!record.IsDBNull(ord))
                data.CategoryID=record.GetInt32(ord);

            ord=record.GetOrdinal("SupplierID");
            if (!record.IsDBNull(ord))
                data.SupplierID=record.GetInt32(ord);

            ord=record.GetOrdinal("ProductName");
            if (!record.IsDBNull(ord))
                data.ProductName=record.GetString(ord);

            ord=record.GetOrdinal("QuantityPerUnit");
            if (!record.IsDBNull(ord))
                data.QuantityPerUnit=record.GetString(ord);

            ord=record.GetOrdinal("UnitPrice");
            if (!record.IsDBNull(ord))
                data.UnitPrice=record.GetDecimal(ord);

            ord=record.GetOrdinal("UnitsInStock");
            if (!record.IsDBNull(ord))
                data.UnitsInStock=record.GetInt16(ord);

            ord=record.GetOrdinal("UnitsOnOrder");
            if (!record.IsDBNull(ord))
                data.UnitsOnOrder=record.GetInt16(ord);

            ord=record.GetOrdinal("ReorderLevel");
            if (!record.IsDBNull(ord))
                data.ReorderLevel=record.GetInt16(ord);

            ord=record.GetOrdinal("Discontinued");
            if (!record.IsDBNull(ord))
                data.Discontinued=record.GetBoolean(ord);

            return data;
        }

        /// <summary />
        private static IDbCommand _CreateSelectCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Product.PrimaryKey key)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.ProductSelect,
                parameterBuilder.GetMarker("ProductID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductID", 1);
            p.DbType=DbType.Int32;
            p.Value=key.Value;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateDeleteCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Product.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.ProductDelete,
                parameterBuilder.GetMarker("ProductID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.ProductID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateInsertCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Product.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.ProductInsert,
                parameterBuilder.GetMarker("ProductID", 1),
                parameterBuilder.GetMarker("CategoryID", 2),
                parameterBuilder.GetMarker("SupplierID", 3),
                parameterBuilder.GetMarker("ProductName", 4),
                parameterBuilder.GetMarker("QuantityPerUnit", 5),
                parameterBuilder.GetMarker("UnitPrice", 6),
                parameterBuilder.GetMarker("UnitsInStock", 7),
                parameterBuilder.GetMarker("UnitsOnOrder", 8),
                parameterBuilder.GetMarker("ReorderLevel", 9),
                parameterBuilder.GetMarker("Discontinued", 10)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.ProductID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CategoryID", 2);
            p.DbType=DbType.Int32;
            p.Value=data.CategoryID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("SupplierID", 3);
            p.DbType=DbType.Int32;
            p.Value=data.SupplierID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductName", 4);
            p.DbType=DbType.String;
            p.Value=data.ProductName;
            p.Size=40;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("QuantityPerUnit", 5);
            p.DbType=DbType.String;
            p.Value=(object)data.QuantityPerUnit ?? (object)DBNull.Value;
            p.Size=20;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("UnitPrice", 6);
            p.DbType=DbType.Decimal;
            p.Value=data.UnitPrice;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("UnitsInStock", 7);
            p.DbType=DbType.Int16;
            p.Value=data.UnitsInStock;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("UnitsOnOrder", 8);
            p.DbType=DbType.Int16;
            p.Value=data.UnitsOnOrder;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ReorderLevel", 9);
            p.DbType=DbType.Int16;
            p.Value=data.ReorderLevel;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Discontinued", 10);
            p.DbType=DbType.Boolean;
            p.Value=data.Discontinued;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateUpdateCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Product.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.ProductUpdate,
                parameterBuilder.GetMarker("CategoryID", 1),
                parameterBuilder.GetMarker("SupplierID", 2),
                parameterBuilder.GetMarker("ProductName", 3),
                parameterBuilder.GetMarker("QuantityPerUnit", 4),
                parameterBuilder.GetMarker("UnitPrice", 5),
                parameterBuilder.GetMarker("UnitsInStock", 6),
                parameterBuilder.GetMarker("UnitsOnOrder", 7),
                parameterBuilder.GetMarker("ReorderLevel", 8),
                parameterBuilder.GetMarker("Discontinued", 9),
                parameterBuilder.GetMarker("ProductID", 10)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CategoryID", 1);
            p.DbType=DbType.Int32;
            p.Value=data.CategoryID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("SupplierID", 2);
            p.DbType=DbType.Int32;
            p.Value=data.SupplierID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductName", 3);
            p.DbType=DbType.String;
            p.Value=data.ProductName;
            p.Size=40;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("QuantityPerUnit", 4);
            p.DbType=DbType.String;
            p.Value=(object)data.QuantityPerUnit ?? (object)DBNull.Value;
            p.Size=20;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("UnitPrice", 5);
            p.DbType=DbType.Decimal;
            p.Value=data.UnitPrice;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("UnitsInStock", 6);
            p.DbType=DbType.Int16;
            p.Value=data.UnitsInStock;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("UnitsOnOrder", 7);
            p.DbType=DbType.Int16;
            p.Value=data.UnitsOnOrder;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ReorderLevel", 8);
            p.DbType=DbType.Int16;
            p.Value=data.ReorderLevel;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Discontinued", 9);
            p.DbType=DbType.Boolean;
            p.Value=data.Discontinued;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ProductID", 10);
            p.DbType=DbType.Int32;
            p.Value=data.ProductID;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private ParameterBuilder _ParameterBuilder;

    }

}
